package com.wc.AlgoOJ_homework.aloj练习赛.F_字符修改;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/8 21:52
 * @description http://43.138.190.70:8888/p/GDCPCA
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 200010;
    static long INF = (long) 4e14;
    // f[i][j][k] 选择到第i个数字前面是数字j的不满足条件的k = 0,已经满足条件 k = 1
    static long[][][] f = new long[N][2][2];
    static int[] a = new int[N];
    static char[] s = new char[N];
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        s = (" " + sc.next()).toCharArray();
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j <= 1; j++) Arrays.fill(f[i][j], INF);
        }
        for (int i = 1; i <= n; i++) a[i] = sc.nextInt();
        f[1][s[1] - '0'][0] = 0;
        f[1][(s[1] - '0') ^ 1][0] = a[1];
        for (int i = 2; i <= n; i++) {
            // 当前不变，选择当前，满足
            f[i][s[i] - '0'][1] = f[i - 1][s[i] - '0'][0];
            // 当前不变，不选择当前，满足
            f[i][s[i] - '0'][1] = Math.min(f[i][s[i] - '0'][1], f[i - 1][(s[i] - '0') ^ 1][1]);

            // 当前变了，选择当前，满足条件
            f[i][(s[i] - '0') ^ 1][1] = f[i - 1][(s[i] - '0') ^ 1][0] + a[i];
            // 当前变了，不选择当前，满足条件
            f[i][(s[i] - '0') ^ 1][1] = Math.min(f[i][(s[i] - '0') ^ 1][1], f[i - 1][s[i] - '0'][1] + a[i]);

            // 当前不变，不满足条件的
            f[i][s[i] - '0'][0] = f[i - 1][(s[i] - '0') ^ 1][0];
            // 当前变了，不满足条件
            f[i][(s[i] - '0') ^ 1][0] = f[i - 1][s[i] - '0'][0] + a[i];
        }
        long res = Math.min(f[n][1][1], f[n][0][1]);
        out.println(res);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
